﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using QLZJFurnitureSCM.Model;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Data;

namespace QLZJFurnitureSCM.DAL
{
    public class ProductDAO:IBaseDAO<Product>, IProductDAO
    {
        private string connectionString = DALFactory.CONNECTIONSTRING;
        private SqlDataAdapter adapter = null;
        private SqlConnection sqlConn = null;

        public ProductDAO()
        {
            sqlConn = new SqlConnection(connectionString);
        }

        public Product Create(Product t)
        {
            try
            {
                string query = string.Format("insert into Products(productName, productPricePurchase, productType, productDescription, productPriceSale, productDimension, productCountAccumulate, productCountSale) values('{0}',{1},{2},'{3}',{4},'{5}',{6}, {7}); select @@identity;", t.ProductName, t
                    .ProductPricePurchase, t.ProductType, t.ProductDescription, t.ProductPriceSale, t.ProductDimension, t.ProductCountAccumulate, t.ProductCountSale);
                if (sqlConn.State != ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand sqlCmd = new SqlCommand(query, sqlConn);
                Console.WriteLine(query);
                object o = sqlCmd.ExecuteScalar();
                sqlConn.Close();
                if (o != null)
                    t.ProductId = int.Parse(o.ToString());
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
                return Create(t);
            }
            return t;
            
        }

        public Product Read(Product t)
        {
            throw new NotImplementedException();
        }

        public bool Update(Product t)
        {
            DataContext dataContext = new DataContext(connectionString);
            try
            {
                string query = string.Format("update Products set ProductCountAccumulate = {0}, ProductPriceSale = {1} where ProductId = {2}; ", t.ProductCountAccumulate, t
                    .ProductPriceSale, t.ProductId);
                Console.WriteLine(query);
                dataContext.ExecuteQuery<Product>(query);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
            return true;
        }

        public bool Delete(Product t)
        {
            throw new NotImplementedException();
        }

        public List<Product> ReadAll()
        {
            DataContext dataContext = new DataContext(connectionString);
            IEnumerable<Product> enumProduct = null;
            try
            {
                enumProduct = dataContext.ExecuteQuery<Product>("select * from Products;");
            }
            catch (System.Exception)
            {
                return null;
            }
            
            return enumProduct.ToList();
        }

        public List<Product> FindProductsByProductType(int productType)
        {
            string query = "select * from Products";
            if (productType != 0)
                query = string.Format(query + " where productType = '{0}';", productType);
            DataContext dataContext = new DataContext(connectionString);
            IEnumerable<Product> enumProduct = dataContext.ExecuteQuery<Product>(query);
            return enumProduct.ToList();
        }

        public DataTable ReadInDataTable(bool descSort)
        {
            string query = "select * from Products";
            if (descSort)
                query = query + " order by productId desc;";
            DataTable dt = null;
            sqlConn.Open();
            dt = new DataTable();
            adapter = new SqlDataAdapter(query, sqlConn);
            adapter.Fill(dt);
            adapter.Dispose();
            sqlConn.Close();
            return dt;
        }

        public bool UpdateDataTable(DataTable dt)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlConn;
            sqlConn.Open();
            adapter = new SqlDataAdapter(cmd);
            if (adapter.Update(dt) > 0)
            {
                adapter.Dispose();
                return true;
            }
            return false;
        }


        public DataTable ReadInDataTableByProductType(int productType)
        {
            string query = "select * from Products";
            if (productType != 0)
                query = string.Format(query + " where productType = '{0}';", productType);
            adapter = new SqlDataAdapter(query, sqlConn);
            DataTable dt = null;
            sqlConn.Open();
            dt = new DataTable();
            adapter.Fill(dt);
            sqlConn.Close();
            return dt;
        }


        public bool UpdateProduct(Product product)
        {
            DataContext dataContext = new DataContext(connectionString);
            try
            {
                string query = string.Format("update Products set ProductName = '{0}', ProductPricePurchase = {1} where ProductId = {2}; ", product.ProductName, product
                    .ProductPricePurchase, product.ProductId);
                dataContext.ExecuteQuery<Product>(query);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
            return true;
            
        }

        public bool UpdateProductCount(Product product)
        {
            DataContext dataContext = new DataContext(connectionString);
            try
            {
                string query = string.Format("update Products set ProductCountAccumulate = ((select ProductCountAccumulate from Products where productId = {1}) - {0}), productCountSale = ((select ProductCountSale from Products where productId = {1}) + {0}) where ProductId = {2}; ", product.ProductCountAccumulate, product.ProductId, product.ProductId);
                Console.WriteLine("Update Query: {0}", query);
                dataContext.ExecuteQuery<Product>(query);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
            return true;
        }

        public bool DeleteProduct(Product product)
        {
            DataContext dataContext = new DataContext(connectionString);
            try
            {
                string query = string.Format("delete Products where ProductId = {0}; ", product.ProductId);
                dataContext.ExecuteQuery<Product>(query);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
            return true;
        }


        public bool DeleteByProductType(int productType)
        {
            DataContext dataContext = new DataContext(connectionString);
            try
            {
                string query = string.Format("delete Products where ProductType = {0}; ", productType);
                Console.WriteLine("delete product: " + query);
                dataContext.ExecuteQuery<Product>(query);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
            return true;
        }


        public bool CheckExistProductsByProductName(Product product)
        {
            try
            {
                string query = string.Format("select * from Products where productName = '{0}' and productType = {1} and productDimension = '{2}';", product.ProductName, product.ProductType, product.ProductDimension
                    );
                Console.WriteLine(query);
                DataContext dataContext = new DataContext(connectionString);
                IEnumerable<Product> enumProduct = dataContext.ExecuteQuery<Product>(query);
                if (enumProduct.Count() > 0)
                    return false;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
            return true;
        }
    }
}
